----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 11103 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Aimed Shot"
SPELL.m_sToolTip =	[[You take aim, firing
					a precise and deadly shot
					towards your target.]]
SPELL.m_sIcon = "devin/blackbetty/spells/ranger/aimedshot.jpg"
SPELL.m_iCoolDown = 12
SPELL.m_iManaCost = 50
SPELL.m_bRequiresTarget = true
SPELL.m_sUsedBy = "class_ranger"

---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )

	local range = self.m_iRange
	
	print("Casted "..self.m_sName.."!")
		
	if(target && target:IsValid() && target != pl) then
		pl:EmitSound("weapons/mortar/mortar_shell_incomming1.wav", 35, 100+math.random(-10,10))
		pl:EmitSound("devin/blackbetty/weapons/castsounds/FireCast.wav", 40, 100+math.random(-10,10))
		ParticleEffectAttach( "impact_firebig", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
		timer.Simple(.9, function()
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
		end)
		net.Start( "SendPlayerCast" )
		local SendTable = {.9, self.m_sName, Color(255, 15, 15, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		pl.DoingCast = true
		pl.CastWhileMoving = true
		pl:EmitSound("devin/blackbetty/weapons/bow/BowPullback0"..math.random(1,3)..".wav", 70, 100+math.random(-20,-10))
		
			timer.Simple(0.9, function()
				if(pl:GetPos():Distance(target:GetPos()) < range)then
					pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_CROSSBOW)
					pl:EmitSound("devin/blackbetty/weapons/bow/BowRelease01.wav", 50, 100+math.random(-10,10))
					pl:EmitSound("weapons/mortar/mortar_fire1.wav", 60, 100+math.random(-10,10))
					if(SERVER)then
						ParticleEffect( "impact_firebig", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), pl )
						local myhitdelay = ((pl:GetPos() - target:GetPos()):Length()/1800)
						LaunchProjectile(pl, target, 0, "pummel_hit", "", "fire_hands", "devin/blackbetty/weapons/travelsounds/FireMissileLoop.wav", myhitdelay, 2400, "models/crossbow_bolt.mdl")
						timer.Simple(myhitdelay, function()
							local dmginfo = DamageInfo()
								dmginfo:SetDamage( 40 ) 
								dmginfo:SetDamageType( DMG_BURN ) 
								dmginfo:SetAttacker( pl )
							target:SetVelocity(((pl:GetPos()- Vector(0, 0, 40)) - target:GetPos())*-2)
							target:TakeDamageInfo( dmginfo )
							target:EmitSound("devin/blackbetty/weapons/bow/ArrowHit0"..math.random(1,3)..".wav", 100, 100+math.random(-10,10))
							pl:EmitSound("weapons/mortar/mortar_explode1.wav", 100, 100+math.random(-10,10))
							ParticleEffect( "impact_firebig", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), pl )
							ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 45), Angle(0,0,0), target )
						end)
					end
				end
			end)
		else
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
			return true
	end
end




